package cc.toprank.syncdatatask.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/***
 * 系统资源工具类 
 * @author  黄小云
 * @date    2015-2-7
 */
public class PropFileHelper {
	private static PropFileHelper instance =  null;
	private static final String SMS_PATH =   "/sms.properties";     // 短信系统配置 
	private static final String CANAL_PATH =   "/canal.properties";     // canal读取bin-log日志配置 
	private static final String MAIL_CONFIG_PATH =   "/mail.properties";  // mail系统配置
	private static final String RABBITMQ_CONFIG_PATH =   "/rabbitmq.properties"; // rabbitmq系统配置
	
	private Properties smsConfigCache = new Properties();  // 短信系统配置  	
	private Properties canalConfigCache = new Properties();  //  canal读取bin-log日志配置 	
	private Properties mailConfigCache = new Properties();  // 读取mail系统配置	
	private Properties rabbitmqConfigCache = new Properties();  // 读取rabbitmq系统配置
	 
	
	private PropFileHelper(){
		
	}
	
	public synchronized static PropFileHelper getInstance() {
		if (instance == null){ 
			instance = new PropFileHelper();
			instance.reload(); 
		}
		return instance;
	}

	/**
	 * 加载配置资源
	 *       
	 * @author      黄小云
	 * @date        2016-2-25
	 */
	private void reload() {
		
		this.loadProperties(canalConfigCache, CANAL_PATH);

	}

	/***
	 * 加载资源文件
	 * @param prop
	 * @param path      
	 * @author      黄小云
	 * @date        2016-2-25
	 */
	private void loadProperties(Properties prop, String path) {
		InputStream is = null;
		try {
			is = PropFileHelper.class.getResourceAsStream(path);
			prop.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if (is != null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 
	
	public String getSmsConfig(String key){
		return smsConfigCache.getProperty(key);
	} 
	
	public String getCanalConfig(String key){
		return canalConfigCache.getProperty(key);
	} 
	
	public String getMailConfig(String key){
		return mailConfigCache.getProperty(key);
	} 
	
	public String getRabbitmqConfig(String key){
		return rabbitmqConfigCache.getProperty(key);
	}


	public static void main(String[] args) {   
		 
		
		System.out.println(PropFileHelper.getInstance().getSmsConfig("sms.sender.url"));
		
		System.out.println(PropFileHelper.getInstance().getCanalConfig("canal.server.ip"));
		
		System.out.println(PropFileHelper.getInstance().getMailConfig("mail.to.email"));
		
		System.out.println(PropFileHelper.getInstance().getRabbitmqConfig("url"));
		
	}

}

